# CREATING VARIABLES FOR YEAR5
######################################
load("claims.14.RData")
attach(claims) # less typing
w <- c(-1, -.5, 0, .5, 1) # possible positions
library(agrmt) # polarization
library(psych) # interpolated median
cy <- c("AT","BE","CH","ES","IE","NL","UK") # countries
yr <- seq(95,105,5)                         # year groups
# gives  "95" for 1995 to 1999
#       "100" for 2000 to 2004
#       "105" for 2005 to 2009 

# migrant actors
claims$mcall <- 0                        # everyone is 0 to start with
claims$mcall[claims$ACTMIGRANT1==1] <- 1 # migrant actor
claims$mcall[claims$ACTMIGRANT2==1] <- 1 # migrant actor
claims$mcall[claims$actor==160] <- 1     # migrant/minority organization; not the 150 CSO

# CSO3
claims$cso1 <- 0                # everyone is 0 to start with
claims$cso1[actor==150] <- 1    # already has all 150x
claims$cso1[actor==160] <- 1    # already has all 160x, except one 1606 claim
claims$cso1[actor==1606] <- 1   # not included in 160
claims$cso1[ACTS1==1504] <- 0   # not the extreme right, but all other 150 yes.

claims$cso2 <- claims$cso1      # begin with definition 1, add more; 1504 thus *not* included
claims$cso2[actor==100] <- 1    # includes all 100x, except 1003
claims$cso2[actor==1003] <- 1

claims$cso3 <- claims$cso1              # begin with definition 1
claims$cso3[claims$ACTMIGRANT1==1] <- 0 # migrant actor : exclude in definition 3
claims$cso3[claims$ACTMIGRANT2==1] <- 0 # migrant actor : exclude
claims$cso3[claims$actor==160] <- 0     # migrant/minority organization : exclude
claims$cso3[claims$actor==1606] <- 0    # migrant/minority organization : exclude

# CSO4
claims$cso4 <- claims$cso2              # begin with definition 2
claims$cso4[claims$ACTMIGRANT1==1] <- 0 # migrant actor : exclude in definition 4
claims$cso4[claims$ACTMIGRANT2==1] <- 0 # migrant actor : exclude
claims$cso4[claims$actor==160] <- 0     # migrant/minority organization : exclude
claims$cso4[claims$actor==1606] <- 0    # migrant/minority organization : exclude

# groups without formal representation (NREP)
claims$nrep <- 0               # everyone else
claims$nrep[OBJACT==2105] <- 1 # asylum seekers
claims$nrep[OBJACT==2106] <- 1 # refugees
claims$nrep[OBJACT==2107] <- 1 # refugees
claims$nrep[OBJACT==2108] <- 1 # illegal

# left-wing parties 
# note, these are now included in more recent version of the "enhanced" SOM data
claims$lw <- 0                       # begin with 0 for everyone
claims$lw[ACTORGPARTYID==21111] <- 1 # left-wing parties
claims$lw[ACTORGPARTYID==21112] <- 1
claims$lw[ACTORGPARTYID==21221] <- 1
claims$lw[ACTORGPARTYID==21322] <- 1
claims$lw[ACTORGPARTYID==22110] <- 1
claims$lw[ACTORGPARTYID==22220] <- 1
claims$lw[ACTORGPARTYID==22320] <- 1
claims$lw[ACTORGPARTYID==22951] <- 1
claims$lw[ACTORGPARTYID==33220] <- 1
claims$lw[ACTORGPARTYID==33320] <- 1
claims$lw[ACTORGPARTYID==33908] <- 1
claims$lw[ACTORGPARTYID==43110] <- 1
claims$lw[ACTORGPARTYID==43320] <- 1
claims$lw[ACTORGPARTYID==51320] <- 1
claims$lw[ACTORGPARTYID==53110] <- 1
claims$lw[ACTORGPARTYID==53221] <- 1
claims$lw[ACTORGPARTYID==53320] <- 1
claims$lw[ACTORGPARTYID==53951] <- 1
claims$lw[ACTORGPARTYID==53999] <- 1
claims$lw[ACTORGPARTYID==42320] <- 1 # Austrian parties not coded as factors, fixed in data
claims$lw[ACTORGPARTYID==42110] <- 1
detach(claims)
attach(claims)

#### polarization
write.csv(
sapply(c("AT","BE","CH","ES","IE","NL","UK"), function(z)
sapply(yr, function(x) polarization(collapse(POSIT[c7 ==z & year5 == x], pos=w)))
)
, file="polarization.csv")

#### salience
load("Newspaper12Jul31.RData") # unmodified SOM data
# real countries:
Newspaper$c7 [Newspaper$country == 1] <- "AT"
Newspaper$c7 [Newspaper$country == 2] <- "BE" # combine Belgian regions
Newspaper$c7 [Newspaper$country == 3] <- "BE"
Newspaper$c7 [Newspaper$country == 4] <- "CH" # combine Swiss regions
Newspaper$c7 [Newspaper$country == 5] <- "CH"
Newspaper$c7 [Newspaper$country == 6] <- "ES"
Newspaper$c7 [Newspaper$country == 7] <- "IE"
Newspaper$c7 [Newspaper$country == 8] <- "NL"
Newspaper$c7 [Newspaper$country == 9] <- "UK"
# year5:
Newspaper$year5 [Newspaper$year >= 1995 & Newspaper$year <= 1999] <- 95
Newspaper$year5 [Newspaper$year >= 2000 & Newspaper$year <= 2004] <- 100
Newspaper$year5 [Newspaper$year >= 2005 & Newspaper$year <= 2009] <- 105
# salience:
ndays <- table(Newspaper$year5,Newspaper$c7)           # number of claims
cclaims <- sapply(cy, function(x) table(year5[c7==x])) # number of claims
max(cclaims/ndays) # 5.17
write.csv(
cclaims/ndays/max(cclaims/ndays)
, file="salience.csv")

#### politicization done manually in a spreadsheet

#### migrant claims
total <-
sapply(cy, function(z)
sapply(yr, function(x) sum(table(cid[c7==z & year5 == x])))
)
# number of claims by migrant actors per country and year
migrant <-
sapply(cy, function(z)
sapply(yr, function(x) sum(table(cid[c7==z & year5 == x & (ACTMIGRANT1 == 1 | ACTMIGRANT2 == 1)])))
)
write.csv(
migrant/total
, file="migrantclaims.csv")

### interpolated median [-1..1]
# all actors
write.csv(
sapply(cy, function (z)
sapply(yr, function(x) interp.median(POSIT[c7==z & year5==x], na.rm=TRUE)/2))
, file="intmed.all.csv")

# intmed for CSO3
write.csv(
sapply(cy, function (z)
sapply(yr, function(x) interp.median(POSIT[c7==z & year5==x & cso3==1], na.rm=TRUE)/2))
, file="intmed.cso3.csv")

# intmed for CSO4
write.csv(
sapply(cy, function (z)
sapply(yr, function(x) interp.median(POSIT[c7==z & year5==x & cso4==1], na.rm=TRUE)/2))
, file="intmed.cso4.csv")

# intmed for LW
write.csv(
sapply(cy, function (z)
sapply(yr, function(x) interp.median(POSIT[c7==z & year5==x & lw==1], na.rm=TRUE)/2))
, file="intmed.lw.csv")

### proportion of claims (any claim) by actor
# CSO3
w <- c(0,1)    # possible positions: 0, 1
csoNum <-
sapply(cy, function (z)
sapply(yr, function(x) collapse(claims$cso3[year5==x & c7==z], pos=w))[2,])
othNum <-
sapply(cy, function (z)
sapply(yr, function(x) collapse(claims$cso3[year5==x & c7==z], pos=w))[1,])
write.csv(
csoNum/(csoNum + othNum) # proportion of claims by CSO
, file="cso3Claims.csv")

# CSO4
csoNum <-
sapply(cy, function (z)
sapply(yr, function(x) collapse(claims$cso4[year5==x & c7==z], pos=w))[2,])
othNum <-
sapply(cy, function (z)
sapply(yr, function(x) collapse(claims$cso4[year5==x & c7==z], pos=w))[1,])
write.csv(
csoNum/(csoNum + othNum) # proportion of claims by CSO
, file="cso4Claims.csv")

# LW
lwNum <-
sapply(cy, function (z)
sapply(yr, function(x) collapse(claims$lw[year5==x & c7==z], pos=w))[2,])
othNum <-
sapply(cy, function (z)
sapply(yr, function(x) collapse(claims$lw[year5==x & c7==z], pos=w))[1,])
write.csv(
lwNum/(lwNum + othNum) # proportion of claims by LW
, file="lwClaims.csv")

### Positive claims
# recoding position variable
claims$position <- NULL
claims$position[POSIT==1] <- 2    # positive
claims$position[POSIT==0.5] <- 2
claims$position[POSIT==0] <- 1    # neutral
claims$position[POSIT==-0.5] <- 0 # negative
claims$position[POSIT==-1] <- 0
detach(claims)
attach(claims)

# CSO3
w <- c(0,1)    # possible positions: 0, 1
posNum <-
sapply(cy, function (z)
sapply(yr, function(x) collapse(claims$cso3[year5==x & c7==z & position==2], pos=w))[2,])
othNum <-
sapply(cy, function (z)
sapply(yr, function(x) collapse(claims$cso3[year5==x & c7==z & (position==1 | position==0)], pos=w))[2,])
write.csv(
posNum/(posNum + othNum) # of the claims by CSO, what proportion is positive?
, file="cso3PosClaims.csv")

# CSO4
posNum <-
sapply(cy, function (z)
sapply(yr, function(x) collapse(claims$cso4[year5==x & c7==z & position==2], pos=w))[2,])
othNum <-
sapply(cy, function (z)
sapply(yr, function(x) collapse(claims$cso4[year5==x & c7==z & (position==1 | position==0)], pos=w))[2,])
write.csv(
posNum/(posNum + othNum) # of the claims by CSO, what proportion is positive?
, file="cso4PosClaims.csv")

# LW
posNum <-
sapply(cy, function (z)
sapply(yr, function(x) collapse(claims$lw[year5==x & c7==z & position==2], pos=w))[2,])
othNum <-
sapply(cy, function (z)
sapply(yr, function(x) collapse(claims$lw[year5==x & c7==z & (position==1 | position==0)], pos=w))[2,])
write.csv(
posNum/(posNum + othNum) # of the claims by LW, what proportion is positive?
, file="lwPosClaims.csv")

### proportion of CSO claims about NREP
# CSO3
w <- c(0,1)    # possible positions: 0, 1
nrepNum <-
sapply(cy, function (z)
sapply(yr, function(x) collapse(claims$cso3[year5==x & c7==z & nrep==1], pos=w))[2,])
othNum <-
sapply(cy, function (z)
sapply(yr, function(x) collapse(claims$cso3[year5==x & c7==z & nrep==0], pos=w))[2,])
write.csv(
nrepNum/(nrepNum + othNum) # of the claims by CSO, what proportion is about NREP?
, file="cso3NREPClaims.csv")

# CSO4
nrepNum <-
sapply(cy, function (z)
sapply(yr, function(x) collapse(claims$cso4[year5==x & c7==z & nrep==1], pos=w))[2,])
othNum <-
sapply(cy, function (z)
sapply(yr, function(x) collapse(claims$cso4[year5==x & c7==z & nrep==0], pos=w))[2,])
write.csv(
nrepNum/(nrepNum + othNum) # of the claims by CSO, what proportion is about NREP?
, file="cso4NREPClaims.csv")

# LW
nrepNum <-
sapply(cy, function (z)
sapply(yr, function(x) collapse(claims$lw[year5==x & c7==z & nrep==1], pos=w))[2,])
othNum <-
sapply(cy, function (z)
sapply(yr, function(x) collapse(claims$lw[year5==x & c7==z & nrep==0], pos=w))[2,])
write.csv(
nrepNum/(nrepNum + othNum) # of the claims by LW, what proportion is about NREP?
, file="lwNREPClaims.csv")

### claims about IMM (immigrants in general)
# CSO3
a <- sapply(cy, function(z)
sapply(yr, function(x) collapse(cso3[c7==z & year5==x & !is.na(object)], pos=w)[2])) # all claims
# the "& !is.na(object)" part is necessary to remove the claims where no object actor is coded at all; for cells with small numbers (see below for positive NREP claims), this can make a substantial difference. I have tested this with three different approaches to be sure there are not coding errors.
i <- sapply(cy, function(z)
sapply(yr, function(x) collapse(cso3[c7==z & year5==x & object==2100], pos=w)[2])) # IMM claims
write.csv(i/a, file="csoIMM3.csv") #  csoIMM3

# CSO4
a <- sapply(cy, function(z)
sapply(yr, function(x) collapse(cso4[c7==z & year5==x & !is.na(object)], pos=w)[2])) # all claims
i <- sapply(cy, function(z)
sapply(yr, function(x) collapse(cso4[c7==z & year5==x & object==2100], pos=w)[2])) # IMM claims
write.csv(i/a, file="csoIMM4.csv") #  csoIMM4

# LW
a <- sapply(cy, function(z)
sapply(yr, function(x) collapse(lw[c7==z & year5==x & !is.na(object)], pos=w)[2])) # all claims
i <- sapply(cy, function(z)
sapply(yr, function(x) collapse(lw[c7==z & year5==x & object==2100], pos=w)[2])) # IMM claims
write.csv(i/a, file="lwIMM.csv") #  lwIMM

# positive claims about NREP
# of the claims by CSO about NREP, what proportion is positive?

# CSO3
a <- sapply(cy, function(z)
sapply(yr, function(x) collapse(cso3[c7==z & year5==x & nrep==1 & !is.na(position)], pos=w)[2])) # all claims about NREP
i <- sapply(cy, function(z)
sapply(yr, function(x) collapse(cso3[c7==z & year5==x & nrep==1 & position==2], pos=w)[2]))      # claims about NREP that are positive
write.csv(i/a, file="cso3PosNREP.csv")

# CSO4
a <- sapply(cy, function(z)
sapply(yr, function(x) collapse(cso4[c7==z & year5==x & nrep==1 & !is.na(position)], pos=w)[2])) # all claims about NREP
i <- sapply(cy, function(z)
sapply(yr, function(x) collapse(cso4[c7==z & year5==x & nrep==1 & position==2], pos=w)[2]))      # claims about NREP that are positive
write.csv(i/a, file="cso4PosNREP.csv")

# LW
a <- sapply(cy, function(z)
sapply(yr, function(x) collapse(lw[c7==z & year5==x & nrep==1 & !is.na(position)], pos=w)[2])) # all claims about NREP
i <- sapply(cy, function(z)
sapply(yr, function(x) collapse(lw[c7==z & year5==x & nrep==1 & position==2], pos=w)[2]))      # claims about NREP that are positive
write.csv(i/a, file="lwPosNREP.csv")

### mcprop
# number of claims by migrant actors and organizations
a <- sapply(cy, function(y)
sapply(yr, function(x) collapse(mcall[c7==y & year5==x], pos=w)[2]))

b <- sapply(cy, function (y)
sapply(yr, function(x) collapse(mcall[c7==y & year5==x], pos=w)[1]))

# mcprop = proportion of migrant claims of all claims
write.csv(a/b, file="mcprop.csv")
